package com.jiangjiang.eshop.wms.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.wms.convert.SendOutOrderConvert;
import com.jiangjiang.eshop.wms.dao.SendOutOrderMapper;
import com.jiangjiang.eshop.wms.domain.SendOutOrderEntity;
import com.jiangjiang.eshop.wms.dto.SendOutOrderDTO;
import com.jiangjiang.eshop.wms.dto.SendOutOrderItemDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName SendOutOrderService
 * @Description TODO
 * @Author jhw
 * @Date 2021/9/17 22:39
 * @Version 1.0
 **/
@Service
public class SendOutOrderService extends ServiceImpl<SendOutOrderMapper, SendOutOrderEntity> {

    @Autowired
    private SendOutOrderConvert sendOutOrderConvert;
    @Autowired
    private SendOutOrderItemService sendOutOrderItemService;

    /**
     * 发货单数据保存
     * @param sendOutOrderDTO 发货单数据
     *
     */
    public Boolean save(SendOutOrderDTO sendOutOrderDTO) {
        SendOutOrderEntity sendOutOrderEntity = sendOutOrderConvert.convertDTO2Entity(sendOutOrderDTO);
        this.save(sendOutOrderEntity);
        // 保存发货单明细数据
        List<SendOutOrderItemDTO> itemDTOList = sendOutOrderDTO.getSendOutOrderItems()
                .stream()
                .peek(item -> item.setSendOutOrderId(sendOutOrderEntity.getId()))
                .collect(Collectors.toList());

        // 保存发货单条目数据
        sendOutOrderItemService.saveBatch(itemDTOList);
        return true;
    }

    /**
     * 获取发货单数据
     * @param saleDeliveryOrderId 销售出库单id
     * @return 发货单数据
     */
    public SendOutOrderDTO queryBySaleDeliveryOrderId(Long saleDeliveryOrderId) {
        LambdaQueryWrapper<SendOutOrderEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SendOutOrderEntity::getSaleDeliveryOrderId, saleDeliveryOrderId);
        return sendOutOrderConvert.convert2DTO(this.getOne(wrapper));
    }

}
